package com.queclink.ycm.entity.data;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.annotation.JSONField;
import com.queclink.ycm.common.utils.HexUtils;
import com.queclink.ycm.common.utils.StringUtil;
import org.springframework.util.ObjectUtils;


/**
 * @author jim.li
 * @email jim.li@queclink.com
 * @date 2021-12-24 17:30:00
 */
public class Data90 extends Data {

    public String HardwareVersion;
    public String BootloaderVersion;
    public String ModemFirmwareVersion;
    public String BLEBootloaderVersion;
    public String BLEFirmwareVersion;
    public String ECUFirmwareVersion;
    public String OBDFirmwareVersion;
    public String WirepasFirmwareVersion;
    public String WirepasMeshAPIVersion;
    public String MeshAPIVersion;
    public String PlatformVersion;
    public String CoprocessorFirmwareVersion;
    public String FirmwareVersion;
    public String WiFiFirmwareVersion;
    public String ModemHardwareVersion;

    public Data90() {
    }

    @JSONField(
            name = "HardwareVersion"
    )
    public String getVersion(JSONObject jsonObject) {
        StringBuffer sb = new StringBuffer();

        sb.append("80");
        if (jsonObject.containsKey("BootloaderVersion")) {
            String bootloaderVersion = jsonObject.getString("BootloaderVersion");
            int length = bootloaderVersion.length();
            sb.append("05");
            sb.append(HexUtils.intToHex(length, 2));
            sb.append(HexUtils.string2HexUTF8(bootloaderVersion));
        }

        if (jsonObject.containsKey("FirmwareVersion")) {
            String firmwareVersion = jsonObject.getString("FirmwareVersion");
            int length = firmwareVersion.length();
            sb.append("06");
            sb.append(HexUtils.intToHex(length, 2));
            sb.append(HexUtils.string2HexUTF8(firmwareVersion));
        }

        if (jsonObject.containsKey("HardwareVersion")) {
            String hardwareVersion = jsonObject.getString("HardwareVersion");
            int length = hardwareVersion.length();
            sb.append("07");
            sb.append(HexUtils.intToHex(length, 2));
            sb.append(HexUtils.string2HexUTF8(hardwareVersion));
        }


        if (jsonObject.containsKey("ModemFirmwareVersion")) {
            String modemFirmwareVersion = jsonObject.getString("ModemFirmwareVersion");
            int length = modemFirmwareVersion.length();
            sb.append("08");
            sb.append(HexUtils.intToHex(length, 2));
            sb.append(HexUtils.string2HexUTF8(modemFirmwareVersion));
        }

        if (jsonObject.containsKey("BLEBootloaderVersion")) {
            String bleBootloaderVersion = jsonObject.getString("BLEBootloaderVersion");
            int length = bleBootloaderVersion.length();
            sb.append("09");
            sb.append(HexUtils.intToHex(length, 2));
            sb.append(HexUtils.string2HexUTF8(bleBootloaderVersion));
        }

        if (jsonObject.containsKey("BLEFirmwareVersion")) {
            String bleFirmwareVersion = jsonObject.getString("BLEFirmwareVersion");
            int length2 = bleFirmwareVersion.length();
            sb.append("0A");
            sb.append(HexUtils.intToHex(length2, 2));
            sb.append(HexUtils.string2HexUTF8(bleFirmwareVersion));
        }

        if (jsonObject.containsKey("WiFiFirmwareVersion")) {
            String wiFiFirmwareVersion = jsonObject.getString("WiFiFirmwareVersion");
            int length = wiFiFirmwareVersion.length();
            sb.append("0B");
            sb.append(HexUtils.intToHex(length, 2));
            sb.append(HexUtils.string2HexUTF8(wiFiFirmwareVersion));
        }

        if (jsonObject.containsKey("OBDFirmwareVersion")) {
            String obdFirmwareVersion = jsonObject.getString("OBDFirmwareVersion");
            int length = obdFirmwareVersion.length();
            sb.append("0C");
            sb.append(HexUtils.intToHex(length, 2));
            sb.append(HexUtils.string2HexUTF8(obdFirmwareVersion));
        }

        if (jsonObject.containsKey("WirepasFirmwareVersion")) {
            String wirepasFirmwareVersion = jsonObject.getString("WirepasFirmwareVersion");
            int length = wirepasFirmwareVersion.length();
            sb.append("0D");
            sb.append(HexUtils.intToHex(length, 2));
            sb.append(HexUtils.string2HexUTF8(wirepasFirmwareVersion));
        }

        if (jsonObject.containsKey("WirepasMeshAPIVersion")) {
            String wirepasMeshAPIVersion = jsonObject.getString("WirepasMeshAPIVersion");
            int length2 = wirepasMeshAPIVersion.length();
            sb.append("0E");
            sb.append(HexUtils.intToHex(length2, 2));
            sb.append(HexUtils.string2HexUTF8(wirepasMeshAPIVersion));
        }

        if (jsonObject.containsKey("ECUFirmwareVersion")) {
            String ecuFirmwareVersion = jsonObject.getString("ECUFirmwareVersion");
            int length = ecuFirmwareVersion.length();
            sb.append("0F");
            sb.append(HexUtils.intToHex(length, 2));
            sb.append(HexUtils.string2HexUTF8(ecuFirmwareVersion));
        }

        if (jsonObject.containsKey("PlatformVersion")) {
            String platformVersion = jsonObject.getString("PlatformVersion");
            int length = platformVersion.length();
            sb.append("10");
            sb.append(HexUtils.intToHex(length, 2));
            sb.append(HexUtils.string2HexUTF8(platformVersion));
        }

        if (jsonObject.containsKey("CoprocessorFirmwareVersion")) {
            String coprocessorFirmwareVersion = jsonObject.getString("CoprocessorFirmwareVersion");
            int length = coprocessorFirmwareVersion.length();
            sb.append("11");
            sb.append(HexUtils.intToHex(length, 2));
            sb.append(HexUtils.string2HexUTF8(coprocessorFirmwareVersion));
        }

        if (jsonObject.containsKey("ModemHardwareVersion")) {
            String coprocessorFirmwareVersion = jsonObject.getString("ModemHardwareVersion");
            int length = coprocessorFirmwareVersion.length();
            sb.append("12");
            sb.append(HexUtils.intToHex(length, 2));
            sb.append(HexUtils.string2HexUTF8(coprocessorFirmwareVersion));
        }

//        sb.insert(0, HexUtils.intToHex(mask, 2));
        return sb.toString();

    }

    public String getDataID() {
        // data90
        return HexUtils.intToHex(90, 2);
    }

    @Override
    public String combine(JSONObject jsonObject) throws Exception {
        StringBuffer sb = new StringBuffer();

        sb.append(this.getVersion(jsonObject));

        int len = sb.length();
        len = len / 2;
        if (len > 127) {
            sb.insert(0, HexUtils.intToHex(len, 4));
        } else {
            sb.insert(0, HexUtils.intToHex(len, 2));
        }
        sb.insert(0, this.getDataID());
        return sb.toString();
    }

}
